var survey = (function () {

    var pagequestions=[];

    //问题序号
    var questionnum=0;

    //不合法提示类
    var S_REMINDINFO="remindinfo";
    
    var $page = null;
    
    //var offsetTop = 0;
    //var topMap = null;
    
    //提交调查时判断合法性
	function checkIfCurrect(question){
        //移除所有提示信息,每次选中一个
        $(".survey_content", $page).find(".remindinfo").removeClass(S_REMINDINFO);
        var  ismustinput=question.ismustinput;
        var  questiontype=question.questiontype;
        var  type= question.type;
        if(questiontype==='2' || (ismustinput==='0' && question.limit === '-1' && question.max === '-1'))
            return true;
        var container=question.container;
        var checklength;
        //选择题
        if(questiontype==='0'){
            checklength = container.find("input:checked").length;
            //单选
            if ("0" === type){
                if(checklength===0){
                    container.addClass(S_REMINDINFO);
                    return false;
                }
                //下拉
            }else if("2" === type){
                var cvalue=container.find("select").val();
                //如果值为-1，则不合法
                if(cvalue==='-1'){
                    container.addClass(S_REMINDINFO);
                    return false;
                }
                //复选
            }else if("1"===type){
                var limit= question.limit;
                var max= question.max;
                if(limit!=='-1' && max!=='-1' ){
                    if(checklength<limit || checklength>max){
                        container.addClass(S_REMINDINFO);
                        return false;
                    }
                }else if(limit==='-1' && max!=="-1"){
                    if(checklength>max){
                        container.addClass(S_REMINDINFO);
                        return false;
                    }
                }else if(limit!=='-1' && max==="-1"){
                    if(checklength<limit){
                        container.addClass(S_REMINDINFO);
                        return false;
                    }
                } else {
                    if(checklength===0) {
                        container.addClass(S_REMINDINFO);
                        return false;
                    }
                }
            }
        } else if(questiontype==='1'){
            var flag=true;
            for (var i = 0; i < question.rows.length; i++) {
                //如果一个未选中,则直接不合法
                if(container.find("input[name='q_"+question.rows[i].quesionid+"_"+question.rows[i].opid+"']:checked").length===0){
                    flag=false;
                    break;
                }
            }
            if(!flag){
                container.addClass(S_REMINDINFO);
                return false;
            }
            //填空题
        }else if(questiontype==='3'){
            var ismustinput=question.ismustinput;
            //必填
            if(ismustinput==='1'){
                if(container.find("textarea").val()==='')    {
                    container.addClass(S_REMINDINFO);
                    return false;
                }
            }
        }
        return true;
    }

    return {
        //提交调查
        submitsurvey: function(callbackFn){
            var that=this;
            var flag=true;
            var questions;
            var uncorrectqus;
            for(var i=0;i<pagequestions.length;i++){
                questions=pagequestions[i].questions;
                for(var j=0;j<questions.length;j++){
                    flag=checkIfCurrect(questions[j]);
                    if(flag===false)
                    {
                        uncorrectqus=questions[j];
                        break;
                    }
                }
                if(flag===false)
                    break;
            }
            if(!flag){
                //var offset=uncorrectqus.container.offset();
                var off=uncorrectqus.container.off();
                //var scroll=uncorrectqus.container.scroll();
                
                
                //console.log("uncorrectqus----:",uncorrectqus);

                //console.log("offset----:",offset);
                //console.log("off----:",off,off[0].offsetTop);
                
                //var topVal = topMap.get(uncorrectqus.quesionid);
                //console.log("topVal----:",topVal);
                //if(!topVal){
                	//offsetTop=offsetTop + offset.top;
                	//topVal = offsetTop;
                	//topMap.set(uncorrectqus.quesionid,topVal);
                //}
                //var top=offset.top;
                //topMap.set(uncorrectqus.quesionid,offsetTop);


                $(".content", $page).scrollTop(off[0].offsetTop);
                //$("html").scrollTop(top);
                //$(document.body).scrollTop(top);
            }
            
            if(flag){
            	if(!confirm("确认提交?")){
            		return;
            	}
            	Vote.showMsg("正在提交，请稍后...");
            	//var formData = new FormData($("form.surveyform", $page)[0]);
            	var formData = $("form.surveyform", $page).serialize();
            	$.ajax({
            		url: "/mobilemode/apps/e-cology/vote/voteAction.jsp?operate=saveVoting",
            		type: 'POST',
            		data: {
            			operate:'saveVoting',
            			formData:formData
            		},
            		success : function(responseText, textStatus) {
            			Vote.hideMsg();
            			var data = JSON.parse(responseText);
            			if(data.status == "1"){
            				if(typeof(callbackFn) == "function"){
            					callbackFn.call(window);
            				}
            			}else{
            				alert("调查提交失败！msg：" + data.msg);
            			}
            		},
            		error: function(responseText) {
            			alert("调查提交失败！");
            		}
            	});
            }
        },
        generatorIntroduce:function(question){
            var content=question.name;
            var container=$("<div class='survey_question'></div>").html(content===""?"自定义文字":content);
            $(".survey_content", $page).append(container);
            container.show();
            return  container;
        },
        //生成填空题
        generatorBlankfilling:function(question){
            var ismustinput=question.ismustinput;
            var qname=question.name;
            var container = $(".clone", $page).clone();
            container.removeClass("clone");
            var tips="填空";
            //必须
            if(ismustinput==='1'){
                container.find(".require").show();
                tips=tips+",必填";
            }else{
                container.find(".require").hide();
            }
            //添加提示信息
            container.find(".rules").html("("+tips+")");
            //填充标题
            container.find(".survey_quesnamedes").html("<span class='questionnum'>"+questionnum+"、</span>"+(question.name === "" ? "新建问题" : question.name));
            //插入填空区域
            container.find(".survey_options").append("<input type='hidden' name='q_"+question.quesionid+"' value='-100'><textarea name=qother_"+question.quesionid+" rows='3' style='width: 100%;' ></textarea>");

            $(".survey_content", $page).append(container);

            container.show();

            //将容器和问题关联
            question.container=container;

            return  container;
        },
        //生成组合题
        generatorComposionQuestion: function (question) {
            //单选类型
            var type = question.type;
            //是否必需
            var ismustinput=question.ismustinput;
            //提示信息
            var tips="";
            //列选项
            var cols = [];
            //行选项
            var rows = [];
            var option;
            var optiontemp;
            //设置选项信息
            for (var optionorder in  question.options) {
                option = question.options[optionorder];
                optiontemp = option;
                if (option.roworcolumn === '0') {
                    rows.push(optiontemp);
                } else {
                    cols.push(optiontemp);
                }

            }
            //保存行列信息
            question.rows=rows;
            question.cols=cols;

            var container = $(".clone", $page).clone();
            container.removeClass("clone");

            //生成提示信息
            if(type==='0'){
                tips="单选";
            }else if(type==='1'){
                tips="多选";
            }
            //必须
            if(ismustinput==='1'){
                container.find(".require").show();
                tips=tips+",必填";
            }else{
                container.find(".require").hide();
            }
            //添加提示信息
            container.find(".rules").html("("+tips+")");

            //填充标题
            container.find(".survey_quesnamedes").html("<span class='questionnum'>"+questionnum+"、</span>"+(question.name === "" ? "新建问题" : question.name));
            //内容区域
            var content;
            //单选框(radio)
            if ("0" === type || "1" === type) {
                var inputtype;
                if ("0" === type) {
                    inputtype = "radio";
                }
                else {
                    inputtype = "checkbox";
                }
                var tr;
                var td;
                var th;
                content = $("<table class='optionstable' ></table>");
                var coltr = $("<tr><th></th></tr>");
                //添加列头
                for (var i = 0; i < cols.length; i++) {
                    th = $("<th>" + (cols[i].oplabel === '' ? '选项' : cols[i].oplabel) + "</th>");
                    coltr.append(th);
                }
                content.append(coltr);
                //添加行头
                var rowtr;
                for (var i = 0; i < rows.length; i++) {
                    rowtr = $("<tr align='center'><td align='left'>" + (rows[i].oplabel === '' ? '新建问题' : rows[i].oplabel) + "</td></tr>");
                    for (var j = 0; j < cols.length; j++) {
                        td = $("<td><input type='" + inputtype + "' value='"+cols[j].opid+"'  name='q_"+rows[i].quesionid+"_"+rows[i].opid+"' ></td>");
                        rowtr.append(td);
                    }
                    content.append(rowtr);
                }

            }
            container.find(".survey_options").append(content);
            $(".survey_content", $page).append(container);
            container.show();
            //将容器和问题关联
            question.container=container;

            //添加校验信息
            if(ismustinput==='1' || (question.limit !== '-1') || (question.max !== '-1')){

                container.find("input").click(function(){
                    var flag=true;
                    for (var i = 0; i < question.rows.length; i++) {
                        //如果一个未选中,则直接不合法
                        if(container.find("input[name='q_"+question.rows[i].quesionid+"_"+question.rows[i].opid+"']:checked").length===0){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        container.removeClass(S_REMINDINFO);
                    }
                });
            }
            
          //注册文字点击事件
            container.find("td").click(function(e){
                   var target=$(e.target);
                   if(target.attr("type")==='radio' ||  target.attr("type")==='checkbox' || target.attr("type")==='text')
                   return;
                   var previnput=$(this).find("input[type='"+inputtype+"']");
                   if(previnput.length===1){
                       if(previnput.is(":checked")){
                           previnput.prop("checked",false);
                           $(this).removeClass("checked");
                           if(inputtype==='radio' || previnput.val()==='-100')
						   container.find("input[type='text']").hide();
                       }
                       else{
					       previnput.prop("checked",true);
					       if(inputtype === "radio") {
							   $(this).siblings().removeClass("checked");
						   }
						   $(this).addClass("checked");
						   if(previnput.val()==='-100'){
							   var name=previnput.attr("name"); 
							   name=name.replace(/q/g,'qother');
							   var textcon=container.find("input[name='"+name+"']");
							   if(textcon.length===1)
									textcon.show();
							   else{
							     var opinput=$("<input class='otherinput' type='text' name='"+name+"' />");
								 previnput.parents("ul").eq(0).append(opinput);
                                 opinput.click(function(e){
								   e.stopPropagation();
								 });
							   }
									
					        }else{
								 if(inputtype==='radio')
							    container.find("input[type='text']").hide();
							}
					   }
                   }
                 e.preventDefault();
                 e.stopPropagation();
            }) ;
        },
        //生成选择题
        generatorSelectQuestion: function (question) {
            var column = question.perrowcols;
            //单选类型
            var type = question.type;
            //是否必需
            var ismustinput=question.ismustinput;
            //最少多少项
            var limit= question.limit;
            //做多多少项
            var  max=  question.max;

            //提示信息
            var tips="";

            var options = [];
            for (var optionorder in  question.options) {
                options.push(question.options[optionorder]);
            }

            var optiontemp=[];
            for(var j=0;j<options.length;j++){
                optiontemp.push(options[j]);
            }
            //其它输入
            if(question.isother==='1'){
                var optemp={};
                optemp.quesionid=options[0].quesionid;
                //其它
                optemp.opid='-100';
                optemp.oplabel='其它';
                optiontemp.push(optemp);
            }

            options=optiontemp;

            var container = $(".clone", $page).clone();
            container.removeClass("clone");
            container.addClass("simpleselect");
            //生成提示信息
            if(type==='0' || type==='2'){
                tips="单选";
            }else if(type==='1'){
                tips="多选";
            }
            //必须
            if(ismustinput==='1'){
                container.find(".require").show();
                tips=tips+",必填";
            }else{
                container.find(".require").hide();
            }
            //至少项数
            if(""!==limit  && "-1"!==limit && limit){
                tips=tips+",至少"+limit+"项";
            }
            //至多项数
            if(""!==max && "-1"!==max && max){
                tips=tips+",至多"+max+"项";
            }

            //添加提示信息
            container.find(".rules").html("("+tips+")");

            //填充标题
            container.find(".survey_quesnamedes").html("<span class='questionnum'>"+questionnum+"、</span>"+(question.name === "" ? "新建问题" : question.name));
            //内容区域
            var content;
            var votingId = $("input[name='votingid']").val();
            question.imageWidth = question.imageWidth ? question.imageWidth : "100";
			question.imageHeight = question.imageHeight ? question.imageHeight : "80";
            //单选框(radio)
            if ("0" === type || "1" === type) {
                var inputtype;
                var unchecktype;
                var checktype
                if ("0" === type) {
                    inputtype = "radio";
                    unchecktype = "unchecked";
                    checktype = "checked";
                }else {
                    inputtype = "checkbox";
                    unchecktype = "uncheckedbox";
                    checktype = "checkedbox";
                }

                var li;
                content = $("<ul class='options'></ul>");

                //每列显示一个(ul显示)
                if (~~column === 1) {
                    for (var i = 0; i < options.length; i++) {
                        
                        var optionIndex = 4;
				        var imageIndex = 4;
				        var attrIndex = 4;
				        var remarkIndex = 4;  
				        
				        optionIndex = options[i].oinner;    
                        
                        var flag = "";
                    	if(question.toBeChecked){
                    		flag = question.toBeChecked[options[i].opid] ? "checked" : "";
                    	}
                        
                        var _li = "<li class='oddnew'><div class='voting_question_show "+unchecktype+"'><input " + flag + " qid='" + options[i].quesionid + "' oid='" + options[i].opid + "' type='" + inputtype + "' name='q_"+options[i].quesionid+"' value='"+options[i].opid+"'  ><label>" + (options[i].oplabel === "" ? "选项" : options[i].oplabel) + "</label></div>";
                        if(options[i].images){
		            		var images = options[i].images;
		            		_li += "<div class='voting_image_show'>";
		            		for(var j = 0;j < images.length;j++){
		            			_li += "<div class='image_div_show'>" + 
		            				"<a href=\"javascript:void(0)\" unselectable=\"off\" contenteditable=\"false\" linkid=\""+images[j].fid+"\""+
		            				" onclick=\"try{downloads('"+images[j].fid+"','"+images[j].title+"');return false;}catch(e){}\" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px;\"> "+
		            				"<img width=" + question.imageWidth + " height='" + question.imageHeight + "' src='/weaver/weaver.file.FileDownload?votingId=" + votingId + "&fileid=" + images[j].fid + "'/>" +
		            				" </a>" +
		            				"</div>";
		            		}
		            		_li +=	"</div>";
		            		imageIndex = images.length > 0 ? images[0].iinner : imageIndex;
		            	}
		            	if(options[i].attrs){
		            		var attrs = options[i].attrs;
		            		_li += "<div class='voting_attr_show'>";
		            		for(var j = 0;j < attrs.length;j++){
		            			_li += "<div class='attr_pdiv_show'>" +
		            				"<div class='attr_div_show'>" + 
		            				"<a href=\"javascript:void(0)\" unselectable=\"off\" contenteditable=\"false\" linkid=\""+attrs[j].fid+"\""+
					                " onclick=\"try{downloads('"+attrs[j].fid+"','"+attrs[j].title+"');return false;}catch(e){}\" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px;\"> "+attrs[j].title+
					                " </a>" +
		            				"<div class=\"clearboth\"></div>" +
		            				"</div></div>";
		            				
		            		}
		            		_li +=	"</div>";
		            		
		            		attrIndex = attrs.length > 0 ? attrs[0].iinner : attrIndex;
		            	}
		            	if(options[i].remark){
		            		_li += "<div class='voting_remark_show' style='line-height:20px;color:#898989;font-size:12px;'>" + options[i].remark + "</div>";
            				remarkIndex = options[i].remarkorder;
		            	}
		            	_li += "</li>";
                        li = $(_li);
                        
                        for(var j = 0;j <= 4;j++){
		                	if(optionIndex == j){
		                		li.append(li.find(".voting_question_show"));
		                	}
					        if(imageIndex == j){
					        	li.append(li.find(".voting_image_show"));
		                	}
					        if(attrIndex == j){
					        	li.append(li.find(".voting_attr_show"));
		                	}
					        if(remarkIndex == j){
					        	li.append(li.find(".voting_remark_show"));
		                	} 
		                }
                        
                        content.append(li);
                    }
                    //table展示
                } else {
                	var tcol = "";
                    var widthper = (1 / ~~column) * 100;
                    for (var i = 0; i < options.length; i++) {
                    	tcol += "<col width='" + widthper + "%' />";
                    }
                    content = $("<table class='optionstablenew' >"+tcol+"</table>");
                    var tr;
                    var td;
                    for (var i = 0; i < options.length; i++) {
                        if (i % ~~column === 0) {
                            tr = $("<tr></tr>");
                            content.append(tr);
                        }
                        
                        var optionIndex = 4;
				        var imageIndex = 4;
				        var attrIndex = 4;
				        var remarkIndex = 4;    
				        
				        optionIndex = options[i].oinner;        	
		                
		                var flag = "";
                    	if(question.toBeChecked){
                    		flag = question.toBeChecked[options[i].opid] ? "checked" : "";
                    	}
		                
		                var _tdcontent = "<td>" 
		                _tdcontent += "<div class='voting_question_show "+unchecktype+"'><input " + flag + " qid='" + options[i].quesionid + "' oid='" + options[i].opid + "' type='" + inputtype + "' name='q_"+options[i].quesionid+"' value='"+options[i].opid+"' ><label>" + (options[i].oplabel === "" ? "选项" : options[i].oplabel) + "</label></div>";
		                if(options[i].images){
		            		var images = options[i].images;
		            		_tdcontent += "<div class='voting_image_show'>";
		            		for(var j = 0;j < images.length;j++){
		            			_tdcontent += "<div class='image_div_show'>" + 
		            				"<a href=\"javascript:void(0)\" unselectable=\"off\" contenteditable=\"false\" linkid=\""+images[j].fid+"\""+
		            				" onclick=\"try{downloads('"+images[j].fid+"','"+images[j].title+"');return false;}catch(e){}\" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px;\"> "+
		            				"<img width=" + question.imageWidth + " height='" + question.imageHeight + "' src='/weaver/weaver.file.FileDownload?votingId=" + votingId + "&fileid=" + images[j].fid + "'/>" +
		            				" </a>" +
		            				"</div>";
		            		}
		            		_tdcontent +=	"</div>";
		            		
		            		imageIndex = images.length > 0 ? images[0].iinner : imageIndex;
		            	}
		            	if(options[i].attrs){
		            		var attrs = options[i].attrs;
		            		_tdcontent += "<div class='voting_attr_show'>";
		            		for(var j = 0;j < attrs.length;j++){
		            			_tdcontent += "<div class='attr_pdiv_show'>" +
		            				"<div class='attr_div_show'>" + 
		            				"<a href=\"javascript:void(0)\" unselectable=\"off\" contenteditable=\"false\" linkid=\""+attrs[j].fid+"\""+
					                " onclick=\"try{downloads('"+attrs[j].fid+"','"+attrs[j].title+"');return false;}catch(e){}\" style=\"cursor:pointer;text-decoration:underline !important;margin-right:8px;\"> "+attrs[j].title+
					                " </a>" +
		            				"<div class=\"clearboth\"></div>" +
		            				"</div></div>";
		            				
		            		}
		            		_tdcontent +=	"</div>";
		            		
		            		attrIndex = attrs.length > 0 ? attrs[0].iinner : attrIndex;
		            	}
		            	if(options[i].remark){
		            		_tdcontent += "<div class='voting_remark_show' style='line-height:20px;color:#898989;font-size:12px;'>" + options[i].remark + "</div>";
		            		
		            		remarkIndex = options[i].remarkorder;
		            	}	
		                _tdcontent += "</td>";
		                td = $(_tdcontent);
		                
		                for(var j = 0;j <= 4;j++){
		                	if(optionIndex == j){
		                		td.append(td.find(".voting_question_show"));
		                	}
					        if(imageIndex == j){
					        	td.append(td.find(".voting_image_show"));
		                	}
					        if(attrIndex == j){
					        	td.append(td.find(".voting_attr_show"));
		                	}
					        if(remarkIndex == j){
					        	td.append(td.find(".voting_remark_show"));
		                	} 
		                }
                        tr.append(td);
                    }
                }
            }
            //下拉单选
            else if (type === '2') {
                content = $("<select class='qs_select'  name='q_"+options[0].quesionid+"' ><option value='-1'>--请选择--</option></select>");
                var newop;
                for (var i = 0; i < options.length; i++) {
                    newop = $("<option value='"+options[i].opid+"' >" + options[i].oplabel + "</option>");
                    content.append(newop);
                }
            }
            container.find(".survey_options").append(content);
            $(".survey_content", $page).append(container);
            container.show();
            //将容器和问题关联
            question.container=container;
            
            //注册文字点击事件
           /* container.find("td").click(function(e){
                   var target=$(e.target);
                   if(target.attr("type")==='radio' ||  target.attr("type")==='checkbox' || target.attr("type")==='text')
                   return;
                   var previnput=$(this).find("input[type='"+inputtype+"']");
                   if(previnput.length===1){
                       if(previnput.is(":checked")){
                           previnput.prop("checked",false);
                           $(this).removeClass("checked");
                           if(inputtype==='radio' || previnput.val()==='-100')
						   container.find("input[type='text']").hide();
						 }
                       else{
					       previnput.prop("checked",true);
					       if(inputtype === "radio") {
					    	   $(this).parents("table").find("td").removeClass("checked");
						   }
						   $(this).addClass("checked");
						   if(previnput.val()==='-100'){
							   var name=previnput.attr("name"); 
							   name=name.replace(/q/g,'qother');
							   var textcon=container.find("input[name='"+name+"']");
							   if(textcon.length===1)
									textcon.show();
							   else{
							     var opinput=$("<input class='otherinput' type='text' name='"+name+"' >");
								 previnput.parents("ul").eq(0).append(opinput);
                                 opinput.click(function(e){
								   e.stopPropagation();
								 });
							   }
					        }else{
								 if(inputtype==='radio')
							    container.find("input[type='text']").hide();
							}
					   }
                         
                   }
                 e.preventDefault();
                 e.stopPropagation();
            }) ;*/
            container.find(".voting_question_show").click(function(e){
                var target=$(e.target);
                if(target.attr("type")==='radio' ||  target.attr("type")==='checkbox' || target.attr("type")==='text')
                    return;
                var previnput=$(this).find("input[type='"+inputtype+"']");
                //console.log("previnput------:",previnput);
                if(previnput.length===1){
                    if(previnput.is(":checked")){
                        previnput.prop("checked",false);
                        $(this).removeClass(checktype);
                        $(this).addClass(unchecktype);
						if(inputtype==='radio' || previnput.val()==='-100')
						container.find("input[type='text']").hide();
					}					    
					else{
					   previnput.prop("checked",true);
					   if(inputtype === "radio" && ~~column === 1) {
						   $(this).parent().siblings().children(".voting_question_show").removeClass(checktype);
						   $(this).parent().siblings().children(".voting_question_show").addClass(unchecktype);
					   }else if(inputtype === "radio") {
						   //$(this).parent().siblings().children().removeClass("checked");
						   //$(this).parent().parent().siblings().children().children().removeClass("checked");
						   $(this).parents(".optionstablenew").find(".voting_question_show").removeClass(checktype);
						   $(this).parents(".optionstablenew").find(".voting_question_show").addClass(unchecktype);
					   }
					   $(this).removeClass(unchecktype);
					   $(this).addClass(checktype);
					   if(previnput.val()==='-100'){
							   var name=previnput.attr("name"); 
							   name=name.replace(/q/g,'qother');
							   var textcon=container.find("input[name='"+name+"']");
							   if(textcon.length===1){
									textcon.show();
							   }else{
							     var opinput=$("<input class='otherinput' type='text' name='"+name+"' >");
								 previnput.parent().append(opinput);
                                 opinput.click(function(e){
									   e.stopPropagation();
								 });
							   }	
					   }else{
						     if(inputtype==='radio')
							 container.find("input[type='text']").hide();
					   }
					}
                }
                e.preventDefault();
                e.stopPropagation();
            }) ;

            if(question.isother==='1'){
                if(type==='0' || type==='1')    {
                    //单选有填空
                    container.find("input[type='"+inputtype+"']").click(function(e){
                        var target=$(e.target);
                        if(target.val()!=='-1' &&  inputtype!=='checkbox'){
                            container.find("input[type='text']").hide();
                        }
                    });
                    container.find("input[type='"+inputtype+"'][value='-100']").click(function(){
                        var current=$(this);
                        var name=current.attr("name");
                        name=name.replace(/q/g,'qother');
                        var textcon=container.find("input[name='"+name+"']");
                        if(current.is(":checked")){
                            if(textcon.length===1)
                                textcon.show();
                            else
                                current.parent().append("<input class='otherinput' type='text' name='"+name+"' >");
                        }else{
                            textcon.hide();
                        }
                    });
                    //下拉其它
                } else if(type==='2'){
                    container.find(".qs_select").change(function(){
                        var current=$(this);
                        var name=current.attr("name");
                        name=name.replace(/q/g,'qother');
                        var textcon=container.find("input[name='"+name+"']");
                        //其它
                        if(current.val()==='-100'){
                            if(textcon.length===1){
                                textcon.show();
                            }else{
                                current.parent().append("<input class='otherinput' type='text' name='"+name+"' >");
                            }
                        }else{
                            textcon.hide();
                        }
                    });
                }
            }

            //添加校验信息
            if(ismustinput==='1' || (question.limit !== '-1') || (question.max !== '-1')){
                //单选
                if ("0" === type ){
                    container.find("input").click(function(){
                        if(container.find("input:checked").length===1){
                            //不合法提示信息
                            container.removeClass("remindinfo");
                        }
                    });
                    //多选
                }else if("1" === type){
                    container.find("input").click(function(){
                        var checkedlength=container.find("input:checked").length;
                        if( "-1"!==limit  && "-1"!=max  &&  limit<=max)
                        {
                            if(checkedlength>=limit  &&  checkedlength<=max){
                                //不合法提示信息
                                container.removeClass("remindinfo");
                            }
                        }else if("-1"!==limit  && "-1"===max){
                            if(checkedlength>=limit){
                                //不合法提示信息
                                container.removeClass("remindinfo");
                            }
                        }else if("-1"===limit  && "-1"!==max){
                            if(checkedlength<=max){
                                //不合法提示信息
                                container.removeClass("remindinfo");
                            }
                        }else {
                            if(checkedlength>=1){
                                //不合法提示信息
                                container.removeClass("remindinfo");
                            }
                        }
                    });
                    //下拉框
                }else if(type==='2'){
                    container.find("select").change(function(){
                        var value=$(this).val();
                        if(value!=='-1'){
                            container.removeClass("remindinfo");
                        }
                    });
                }
            }
        },
        //生成问题
        generatorQuestion: function (question) {
            var that = this;
            //获取问题类型,根据不同的问题生成对应的选项
            var questiontype = question.questiontype;
            //选择题
            if (questiontype === "0") {
                questionnum++;
                that.generatorSelectQuestion(question);
            } else if (questiontype === '1') {
                //多选题
                questionnum++;
                that.generatorComposionQuestion(question);
            } else if (questiontype === '2') {
                //说明
                that.generatorIntroduce(question);
            } else if (questiontype === '3') {
                //填空
                questionnum++;
                that.generatorBlankfilling(question);
            }
        },
        //初始化调查
        initSurvey: function (pageitems,surveytitle,page_tag) {
            var that = this;
            $page = $("#" + page_tag);
            //offsetTop=0;
            //topMap = new Map();
            var page;
            var question;
            questionnum=0;
            $(".survey_title", $page).html(surveytitle===""?"新建调查":surveytitle);
            var pageinfo;
            var questioninfo;
            pagequestions = [];
            for (var pagenum in pageitems) {
                page = pageitems[pagenum];
                pageinfo={};
                pageinfo.pagenum=pagenum;
                pageinfo.questions=[];
                for (var quesnum in page) {
                    question = page[quesnum];
                    pageinfo.questions.push(question);
                    that.generatorQuestion(question);
                    pagequestions.push(pageinfo);
                }
            }
        },
        //恢复用户输入的调查数据
        fillUserInput:function(userinputs){
            //填写选择题
            function fillQtype0(vote){
                var qdtype=vote.qdtype;
                var isother=vote.qother;
                var inputname;
                var inputvalue;
                //其它输入
                if(isother==='1' &&  vote.isothervalue==='1'){
                    inputname="q_"+vote.qid;
                    inputvalue="qother_"+vote.qid;
                    //选中其它
//                    $("input[name='"+inputname+"'][value='-100']").prop("checked",true);
//                    $("input[name='"+inputname+"'][value='-100']").trigger("click");
                    if(qdtype==='0' || qdtype==='1') {
                    	$("input[name='"+inputname+"'][value='-100']", $page).parent().triggerHandler("click");
                    }else if(qdtype==='2'){
                    	$("select[name='"+inputname+"']", $page).val("-100").change();
                    }
                    //赋值
                    $("input[name='"+inputvalue+"']", $page).val(vote.qvalue);
                    return;
                }
                //input框名称
                inputname="q_"+vote.qid;
                inputvalue=vote.qvalue;
                //单选或复选(直接设置checked)
                if(qdtype==='0' || qdtype==='1'){
                    //设置选中
//                    $("input[name='"+inputname+"'][value='"+inputvalue+"']").prop("checked",true);
					$("input[name='"+inputname+"'][value='"+inputvalue+"']", $page).parent().triggerHandler("click");
                    //下拉
                }else if(qdtype==='2'){
                    $("select[name='"+inputname+"']", $page).val(inputvalue);
                }
            }
            //组合选择
            function fillQtype1(vote){
                var qid=vote.qid;
                var qvalues=vote.qvalue.split("_");
                var inputname="q_"+qid+"_"+qvalues[0];
                var inputvalue=qvalues[1];
//                $("input[name='"+inputname+"'][value='"+inputvalue+"']").prop("checked",true);
				$("input[name='"+inputname+"'][value='"+inputvalue+"']", $page).parent().triggerHandler("click");
            }
            //填空选择
            function fillQtype3(vote){
                var inputname="qother_"+vote.qid;
                $("textarea[name='"+inputname+"']", $page).val(vote.qvalue);
            }
            var qtype;
            for(var i=0;i<userinputs.length;i++){
                //问题类型
                qtype=userinputs[i].qtype;
                switch (qtype){
                    case '0': fillQtype0(userinputs[i]);break;
                    case '1': fillQtype1(userinputs[i]);break;
                    case '3': fillQtype3(userinputs[i]);break;
                    default :break;
                }
            }
            //var bheight=$(".survey_preview_content", $page).height();
            //var cover = $("<div class=\"cover\" style='position: absolute; width: 100%; height: " + bheight + "px; z-index: 10000; left: 0px; top: 0px; overflow:auto;'></div>");
            //$(".content", $page).append(cover);
	  
            $("input[type='radio'],input[type='checkbox'],textarea").attr("disabled",true);
            $(".voting_question_show").unbind("click");
            $("table.optionstable td").unbind("click");
        }
    };
})();

function downloads(fileid,filename){
   	filename = filename?filename:"";
	location = "/download.do?fileid="+fileid+"&filename="+encodeURI(filename)+"&module=-2&scope=-301";
}